﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.kiosk {
    import reprise.ui.*;
    import flash.events.*;
    import flash.display.*;
    import reprise.controls.*;
    import flash.text.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.fountain.*;
    import net.wooga.pets.models.kiosk.*;
    import net.wooga.pets.models.world.*;

    public class AbstractKioskTabMediator extends Mediator {

        public var language:LanguageModel;
        public var symbolModel:SymbolModel;
        public var kioskModel:KioskModel;
        public var userModel:UserModel;
        public var worldModel:WorldModel;
        public var paymentModel:PaymentModel;
        public var contractsModel:ContractsModel;
        public var inventoryModel:InventoryModel;
        public var sessionModel:SessionModel;
        public var pendingModel:PendingModel;
        private var _category:String;
        private var _kiosk:IRoom;
        private var _getButtons:Array;
        private var _icons:Array;
        private var _selectedTab:String;
        private var _buyClips:Dictionary;

        protected static const ITEM_CATEGORY:String = "Item";
        protected static const TOY_CATEGORY:String = "Toy";
        protected static const COFFEE_CATEGORY:String = "Coffee";
        protected static const CRAFTED_ICON_MARGIN:int = 5;

        public function AbstractKioskTabMediator(){
            this._getButtons = [];
            this._icons = [];
            this._buyClips = new Dictionary();
            super();
        }
        override public function onRegister():void{
            super.onRegister();
            addContextListener(ProtocolConstants.POW_PUBLISH, this.powUpdate, InfoEvent);
            addContextListener(ProtocolConstants.POW_CANCEL, this.powUpdate, InfoEvent);
            addContextListener(EventConstants.UPDATE_ASK_FOR_GIFT_BUTTON, this.onUpdateAskForGiftButton, InfoEvent);
            addContextListener(EventConstants.PLAY_KIOSK_BUY_ANIMATION, this.onFBItemBought, InfoEvent);
            addContextListener(UserModelEvent.ON_MODEL_CHANGED, this.onModelChanged, UserModelEvent);
            var _local1:String = String(this.worldModel.getRoomIdsOfType(GameConstants.TYPE_KIOSK));
            this._kiosk = (this.worldModel.getRoomById(_local1) as IRoom);
        }
        override public function onRemove():void{
            super.onRemove();
            this._buyClips = null;
        }
        protected function createButton(_arg1:UIComponent, _arg2:String, _arg3:String, _arg4:String, _arg5:Object=null):LabelButton{
            return (RepriseUtils.addLabelButton(_arg1, _arg2, this.language.getTextByKey(_arg3, _arg5), null, null, _arg4));
        }
        protected function addLabel(_arg1:String, _arg2:String, _arg3:Object=null, _arg4:UIComponent=null, _arg5:String=null):void{
            var _local6:String = this.language.getTextByKey(_arg2, _arg3);
            RepriseUtils.addLabel(((_arg4) || (this.view)), _arg1, _local6, _arg5);
        }
        protected function createNotAvailableProduct(_arg1:UIComponent, _arg2:String):void{
            this.addLabel("notAvailableLabel myriadProFontOutline", _arg2, null, _arg1);
        }
        private function get view():AbstractKioskTabView{
            return ((viewComponent as AbstractKioskTabView));
        }
        protected function createRacks():void{
            var _local2:UIComponent;
            var _local3:Object;
            var _local1:Array = this.kioskModel.productList;
            for each (_local3 in _local1) {
                if (_local3.category == this.category){
                    _local2 = RepriseUtils.addComponent(this.view, "itemRack");
                    if (_local3.level <= this.userModel.level){
                        if (_local3.type == GameConstants.REWARD_COFFEE){
                            this.createSingleCoffeeItem(_local2, _local3);
                        } else {
                            this.createCraftingItem(_local2, _local3);
                            this.createNeededItems(_local2, _local3);
                        };
                    } else {
                        this.createLock(_local2, _local3.level);
                    };
                };
            };
        }
        private function createSingleCoffeeItem(_arg1:UIComponent, _arg2:Object):void{
            var _local3:UIComponent = this.createCraftedItemContainer(_arg1);
            var _local4:int = _arg2.required_items[0].amount;
            var _local5:ClipComponent = this.createCraftingIcon(_local3, _arg2);
            this.setBuyAnimation(_local3, _arg2);
            var _local6:Boolean = this.handleProductAvailability(_arg2, _local3);
            if (!_local6){
                _local5.filters = [Filters.getFilter(Filters.DESATURATE)];
            } else {
                this.addGetButton(_local3, _arg2, "singleCoffeButton kioskButton greenButton getButton", true);
                eventMap.mapListener(_local5, MouseEvent.CLICK, this.onGetClick);
                eventMap.mapListener(_local5, MouseEvent.MOUSE_OVER, this.onIconMouseOver);
                eventMap.mapListener(_local5, MouseEvent.MOUSE_OUT, this.onIconMouseOut);
            };
            var _local7:UIComponent = RepriseUtils.addComponent(_arg1, "neededItemContainer");
            RepriseUtils.addComponent(_local7, "Equals");
            var _local8:String = ((this.userModel.coins >= _local4)) ? "neededItem neededItemGreen" : "neededItem";
            var _local9:UIComponent = RepriseUtils.addComponent(_local7, _local8);
            this.addLabel("gemDescription myriadProFontNormal", "global_coins", null, _local9);
            this.addGemIcon(_local9, "Coins");
            RepriseUtils.addLabel(_local9, "neededLabel myriadProFontOutline", _local4.toString());
            (((this.userModel.coins >= _local4)) && (this.addSolvedIcon(_local9)));
        }
        protected function createNeededItems(_arg1:UIComponent, _arg2:Object):void{
            var _local6:Object;
            var _local3:UIComponent = RepriseUtils.addComponent(_arg1, "neededItemContainer");
            RepriseUtils.addComponent(_local3, "Equals");
            var _local4:Array = _arg2.required_items;
            var _local5:int;
            for (;_local5 < _local4.length;_local5++) {
                _local6 = _local4[_local5];
                this.createNeededItem(_local3, _local6);
                if ((_local5 < (_local4.length - 1))){
                    RepriseUtils.addComponent(_local3, "Plus");
                    continue;
                };
            };
        }
        protected function createNeededItem(_arg1:UIComponent, _arg2:Object):void{
            var _local3:NeededCraftItem = (RepriseUtils.addComponent(_arg1, "neededItem", NeededCraftItem) as NeededCraftItem);
            _local3.item = _arg2;
        }
        private function hasRequiredItems(_arg1:Array):Boolean{
            var _local2:int;
            var _local3:int;
            var _local4:Object;
            for each (_local4 in _arg1) {
                _local2 = this.getActualItemCount(_local4);
                _local3 = this.pendingModel.getPendingItemCount(_local4.type);
                if ((_local2 - _local3) < _local4.amount){
                    return (false);
                };
            };
            return (true);
        }
        private function createCraftingIcon(_arg1:UIComponent, _arg2:Object):ClipComponent{
            var content = _arg1;
            var product = _arg2;
            var clip:MovieClip = AssetsLibrary.getMovieClip(("RC_" + product.type));
            var icon:ClipComponent = RepriseUtils.addClipComponent(content, "craftedItem", clip);
            icon.setData(product);
            this._icons.push(icon);
            var items:int = this.getCraftedItemCount(product);
            icon.setTooltipData(this.language.getTextByKey("kiosk_stocked_items", {amount:items}));
            RepriseUtils.validateComponent(icon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.clip.width, _arg1.clip.height, 1, 1);
                _arg1.clip.x = (((_arg1.width / 2) - (_arg1.clip.width / 2)) + CRAFTED_ICON_MARGIN);
                _arg1.clip.y = (_arg1.clip.y + _arg1.height);
            });
            return (icon);
        }
        private function createCraftedItemContainer(_arg1:UIComponent):UIComponent{
            return (RepriseUtils.addComponent(_arg1, "craftedItemContainer"));
        }
        private function getItemPrice(_arg1:String):int{
            var _local6:Object;
            var _local2:Array = (this.paymentModel.getData("buy_craft_items") as Array);
            var _local3:Array = (this.paymentModel.getData("buy_coffee") as Array);
            var _local4:Array = _local2.concat(_local3);
            var _local5:int;
            for each (_local6 in _local4) {
                if (_local6.product_id == _arg1){
                    _local5 = _local6.price;
                    break;
                };
            };
            return (_local5);
        }
        private function createLock(_arg1:UIComponent, _arg2:String):void{
            RepriseUtils.addComponent(_arg1, "lockedBG");
            RepriseUtils.addComponent(_arg1, "levelLocked");
            _arg2 = this.language.getTextByKey("global_levelnumber", {levelnumber:_arg2});
            RepriseUtils.addLabel(_arg1, "showcardGothicFontOutline levelLockedLabel", _arg2);
        }
        protected function createCraftingItem(_arg1:UIComponent, _arg2:Object):void{
            var _local3:Boolean = this.hasRequiredItems(_arg2.required_items);
            var _local4:UIComponent = this.createCraftedItemContainer(_arg1);
            var _local5:int = this.getItemPrice(_arg2.product_id);
            var _local6:ClipComponent = this.createCraftingIcon(_local4, _arg2);
            this.setBuyAnimation(_local4, _arg2);
            var _local7:Boolean = this.handleProductAvailability(_arg2, _local4);
            if (!_local7){
                _local6.filters = [Filters.getFilter(Filters.DESATURATE)];
            } else {
                ((_local5) && (this.addBuyButton(_local4, _arg2, "buyButton blueCashButton")));
                if (_local3){
                    this.addGetButton(_local4, _arg2, "buyButton kioskButton greenButton getButton", true);
                    eventMap.mapListener(_local6, MouseEvent.MOUSE_OVER, this.onIconMouseOver);
                    eventMap.mapListener(_local6, MouseEvent.MOUSE_OUT, this.onIconMouseOut);
                    eventMap.mapListener(_local6, MouseEvent.CLICK, this.onGetClick);
                } else {
                    this.addGetButton(_local4, _arg2, "buyButton kioskButton greyButton getButton", false);
                };
            };
        }
        private function onIconMouseOut(_arg1:MouseEvent):void{
            var _local2:UIComponent = UIComponent(_arg1.currentTarget);
            var _local3:ClipComponent = ClipComponent(_arg1.currentTarget);
            _local2.filters = null;
            this.getGetButtonByData(_local3.data().code).removeCSSPseudoClass("hover");
        }
        private function onIconMouseOver(_arg1:MouseEvent):void{
            var _local2:UIComponent = UIComponent(_arg1.currentTarget);
            var _local3:ClipComponent = ClipComponent(_arg1.currentTarget);
            _local2.filters = [Filters.getFilter(Filters.WHITE_GLOW)];
            this.getGetButtonByData(_local3.data().code).addCSSPseudoClass("hover");
        }
        private function getGetButtonByData(_arg1:String):LabelButton{
            var _local2:Object;
            var _local3:LabelButton;
            for each (_local3 in this._getButtons) {
                _local2 = _local3.data();
                if (String(_local2.code) == _arg1){
                    return (_local3);
                };
            };
            return (null);
        }
        private function handleProductAvailability(_arg1:Object, _arg2:UIComponent):Boolean{
            var _local3:Boolean;
            var _local4:int = this.getAmountOfCollectableItems(_arg1.type);
            if (((this.isTypeCoffee(_arg1.type)) && ((((_arg1.amount + this.userModel.coffee) + _local4) > GameConstants.MAX_INEVNTORY_ITEM_NUM)))){
                this.createNotAvailableProduct(_arg2, "kiosk_stock_full");
                _local3 = false;
            } else {
                if ((_arg1.amount + _local4) > GameConstants.MAX_INEVNTORY_ITEM_NUM){
                    this.createNotAvailableProduct(_arg2, "kiosk_stock_full");
                    _local3 = false;
                } else {
                    if (_arg1.price > this.userModel.coins){
                        this.createNotAvailableProduct(_arg2, "global_no_coins");
                        _local3 = false;
                    };
                };
            };
            return (_local3);
        }
        protected function addAmountLabel(_arg1:UIComponent, _arg2:String):void{
            RepriseUtils.addLabel(_arg1, "multiplierLabel myriadProFontOutline", _arg2);
        }
        protected function addGemIcon(_arg1:UIComponent, _arg2:String):void{
            var target = _arg1;
            var key = _arg2;
            var iconClip:MovieClip = AssetsLibrary.getMovieClip((("Reward" + Strings.capitalize(key)) + "Thumb"));
            var icon:UIComponent = RepriseUtils.addClipComponent(target, "gemContainer", iconClip);
            RepriseUtils.validateComponent(icon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, 0, 0);
            });
        }
        protected function addSolvedIcon(_arg1:UIComponent):void{
            var target = _arg1;
            var solvedClip:MovieClip = AssetsLibrary.getMovieClip("SolvedIcon");
            var solvedIcon:UIComponent = RepriseUtils.addClipComponent(target, "solvedIcon", solvedClip);
            RepriseUtils.validateComponent(solvedIcon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, -1, -1);
            });
        }
        private function getAmountOfCollectableItems(_arg1:String):int{
            var _local2:int;
            var _local3:RewardItemVO;
            for each (_local3 in this._kiosk.rewardItems) {
                if ((((((_arg1 == GameConstants.REWARD_COFFEE)) || ((((_arg1 == GameConstants.REWARD_COFFEE_PACK)) && ((_local3.itemType == GameConstants.REWARD_COFFEE)))))) || ((_local3.itemType == GameConstants.REWARD_COFFEE_PACK)))){
                    _local2 = (_local2 + int(_local3.amount));
                } else {
                    if (_local3.itemType == _arg1){
                        _local2 = (_local2 + int(_local3.amount));
                    };
                };
            };
            return (_local2);
        }
        private function isTypeCoffee(_arg1:String):Boolean{
            return ((((_arg1 == GameConstants.REWARD_COFFEE)) || ((_arg1 == GameConstants.REWARD_COFFEE_PACK))));
        }
        private function getCraftedItemCount(_arg1:Object):int{
            var _local2:String = _arg1.type;
            if ((((_arg1.type == GameConstants.REWARD_COFFEE)) || ((_arg1.type == GameConstants.REWARD_COFFEE_PACK)))){
                _local2 = GameConstants.COFFEE;
                return (this.userModel.getItemsByType(_local2));
            };
            return (this.inventoryModel.getItemNumberByKey(_local2));
        }
        private function getActualItemCount(_arg1:Object):int{
            var _local2:int;
            switch (_arg1.type){
                case GameConstants.GOODS_ASSETS:
                    _local2 = this.userModel.goods;
                    break;
                case ProtocolConstants.COINS:
                    _local2 = this.userModel.coins;
                    break;
                default:
                    _local2 = this.symbolModel.symbols[_arg1.type];
                    break;
            };
            return (_local2);
        }
        protected function addBuyButton(_arg1:UIComponent, _arg2:Object, _arg3:String):void{
            var _local4:LabelButton = this.createButton(_arg1, _arg3, "symbols_buy_button", "myriadProFont");
            _local4.setData({id:_arg2.product_id, code:_arg2.code});
            eventMap.mapListener(_local4, MouseEvent.CLICK, this.onBuyClick);
        }
        protected function addGetButton(_arg1:UIComponent, _arg2:Object, _arg3:String, _arg4:Boolean):void{
            var _local5:LabelButton = this.createButton(_arg1, _arg3, "global_amount_get", "myriadProFont", {amount:_arg2.amount});
            _local5.setData(_arg2);
            _local5.enabled = _arg4;
            this._getButtons.push(_local5);
            eventMap.mapListener(_local5, MouseEvent.CLICK, this.onGetClick);
            eventMap.mapListener(_local5, MouseEvent.MOUSE_OVER, this.onGetButtonMouseOver);
            eventMap.mapListener(_local5, MouseEvent.MOUSE_OUT, this.onGetButtonMouseOut);
        }
        private function onGetButtonMouseOut(_arg1:MouseEvent):void{
            var _local2:LabelButton = LabelButton(_arg1.currentTarget);
            var _local3:Object = _local2.data();
            this.getIconByData(_local3).filters = null;
        }
        private function onGetButtonMouseOver(_arg1:MouseEvent):void{
            var _local2:LabelButton = LabelButton(_arg1.currentTarget);
            var _local3:Object = _local2.data();
            this.getIconByData(_local3).filters = [Filters.getFilter(Filters.WHITE_GLOW)];
        }
        private function getIconByData(_arg1:Object):ClipComponent{
            var _local2:ClipComponent;
            for each (_local2 in this._icons) {
                if (String(_local2.data()) == _arg1){
                    return (_local2);
                };
            };
            return (null);
        }
        protected function onGetClick(_arg1:MouseEvent):void{
            var _local2:Object = _arg1.currentTarget.data();
            var _local3:InfoEvent = new InfoEvent(GameConstants.CRAFT_PRODUCT, this._kiosk.id, _local2);
            dispatch(_local3);
            this.playBuyAnimation(_local2.code);
            this.udpateButtonStates();
        }
        protected function onAskClick(_arg1:MouseEvent):void{
            var _local2:String = _arg1.currentTarget.data();
            var _local3:InfoEvent = new InfoEvent(GameConstants.ASK_FOR_SYMBOL, _local2);
            dispatch(_local3);
        }
        protected function onBuyClick(_arg1:MouseEvent):void{
            var _local2:Object = _arg1.currentTarget.data();
            var _local3:InfoEvent = new InfoEvent(GameConstants.BUY_KIOSK_PRODUCT, _local2.id, _local2);
            dispatch(_local3);
        }
        protected function closePopup():void{
            var _local1:InfoEvent = new InfoEvent(EventConstants.CLOSE_POPUP_BY_TYPE, GameConstants.KIOSK_POPUP);
            dispatch(_local1);
        }
        public function get category():String{
            return (this._category);
        }
        public function set category(_arg1:String):void{
            this._category = _arg1;
        }
        private function onUpdateAskForGiftButton(_arg1:InfoEvent):void{
            var _local2:String = _arg1.info.type;
            this.handleRequestLimits(_local2);
        }
        private function powUpdate(_arg1:InfoEvent):void{
            var _local5:String;
            var _local2:Array = (_arg1.info as Array);
            var _local3:Object = _local2[0];
            var _local4:String = _local3.feed_type;
            if ((((_arg1.type == ProtocolConstants.POW_PUBLISH)) && ((_local4 == GameConstants.ASK_FOR_SYMBOL)))){
                _local5 = _local3.optional_type;
                this.handleRequestLimits(_local5);
            };
        }
        private function handleRequestLimits(_arg1:String):void{
            var _local3:LabelButton;
            this.sessionModel.setRequestLimit(_arg1, true);
            var _local2:Array = this.view.getElementsByClassName("kioskButton");
            for each (_local3 in _local2) {
                if (_local3.data() == _arg1){
                    _local3.remove();
                };
            };
        }
        private function onModelChanged(_arg1:UserModelEvent):void{
            this.udpateButtonStates();
        }
        private function udpateButtonStates():void{
            var _local1:LabelButton;
            var _local2:Object;
            for each (_local1 in this._getButtons) {
                _local2 = _local1.data();
                if (!this.hasRequiredItems(_local2.required_items)){
                    _local1.removeCSSClass("greenButton");
                    _local1.addCSSClass("greyButton");
                    _local1.enabled = false;
                };
            };
        }
        private function onFBItemBought(_arg1:InfoEvent):void{
            this.playBuyAnimation(_arg1.id);
        }
        private function setBuyAnimation(_arg1:UIComponent, _arg2:Object):void{
            var _local3:MovieClip = AssetsLibrary.getMovieClip("RC_kioskBuyAnimation");
            var _local4:String = _arg2.code;
            var _local5:String = _arg2.type.replace("Reward", "");
            var _local6:String = ("global_amount_" + _local5.toLowerCase());
            var _local7:String = ("+ " + this.language.getTextByKey(_local6, {amount:_arg2.amount}));
            var _local8:TextFormat = new TextFormat();
            _local8.font = "MyriadProFont";
            _local8.size = 18;
            _local8.bold = true;
            var _local9:ClipComponent = RepriseUtils.addClipComponent(_arg1, "buyAnimation", _local3);
            var _local10:MovieClip = (_local9.clip as MovieClip);
            _local10.visible = false;
            _local10.mouseChildren = false;
            this._buyClips[_local4] = _local10;
            if (_local10.rewardText){
                _local10.rewardText.tf.embedFonts = true;
                _local10.rewardText.tf.htmlText = _local7;
                _local10.rewardText.tf.autoSize = TextFieldAutoSize.CENTER;
                _local10.rewardText.tf.multiline = false;
                _local10.rewardText.tf.setTextFormat(_local8);
            };
        }
        private function playBuyAnimation(_arg1:String):void{
            var _local2:MovieClip = this._buyClips[_arg1];
            if (_local2){
                _local2.visible = true;
                _local2.gotoAndPlay("start");
                _local2.stars.gotoAndPlay("start");
            };
            dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_SELL_SOMETHING));
        }

    }
}//package net.wooga.pets.views.popups.kiosk 
